package mathive.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
import com.google.gwt.user.client.ui.Label;

public class LoginSidebar extends SidebarObject {
	private static final int BUTTON_LOGIN = 0;
	private static final int BUTTON_LOGOUT = 1;
	private static final int REGISTER = 2;
	private static final int FORGOT = 3;
	private static final int CHANGE = 4;
	private static final int TEST = 5;
	private static final int SUDOKU = 6;
	private static final int USERS = 7;
	
	private static final String LOGIN_BUTTON_TEXT = "Login";
	private static final String DEFAULT_HEIGHT = "150px";
	
	private Button loginButton;
	private Button logoutButton;
	
	private FormPanel form;
	
	private VerticalPanel content;
	private VerticalPanel loggedIn;
	private VerticalPanel loggedOut;
	
	private Label register;
	private Label forgotPass;
	private Label changePass;
	private Label createSudoku;
	private Label handleUsers;
	private Label manageTests;
	
	private Label score;
	
	
	public LoginSidebar() {
		content = new VerticalPanel();
		content.add(new Label("Loading..."));
		
		loggedIn = new VerticalPanel();
		loggedOut = new VerticalPanel();
		
		setHeight(DEFAULT_HEIGHT);
		
		loginButton = new Button(LOGIN_BUTTON_TEXT);
		loginButton.addClickHandler(new ButtonHandler(BUTTON_LOGIN));
		logoutButton = new Button("Logout");
		logoutButton.addClickHandler(new ButtonHandler(BUTTON_LOGOUT));
		
		register = new Label("Register");
		register.addClickHandler(new ButtonHandler(REGISTER));
		register.addStyleName("cursor");
		forgotPass = new Label("Forgot password");
		forgotPass.addClickHandler(new ButtonHandler(FORGOT));
		forgotPass.addStyleName("cursor");
		changePass = new Label("Change password");
		changePass.addClickHandler(new ButtonHandler(CHANGE));
		changePass.addStyleName("cursor");
		manageTests = new Label("Manage tests");
		manageTests.addClickHandler(new ButtonHandler(TEST));
		manageTests.addStyleName("cursor");
		createSudoku = new Label("Create new Sudoku");
		createSudoku.addClickHandler(new ButtonHandler(SUDOKU));
		createSudoku.addStyleName("cursor");
		handleUsers = new Label("Handle users");
		handleUsers.addClickHandler(new ButtonHandler(USERS));
		handleUsers.addStyleName("cursor");
		
		score = new Label("");
		
		form = FormPanel.wrap(DOM.getElementById("login"), true);
		form.addSubmitHandler(new SubmitHandler());
		
		//form.add(loginButton);
		loggedOut.add(form);
		loggedOut.add(register);
		loggedOut.add(forgotPass);
		loggedIn.add(score);
		loggedIn.add(logoutButton);
		loggedIn.add(changePass);
		
		add(content);
		
//		update();
		
		add(new Label(QueryString.getString()));
	}
	
	public void login() {
		form.submit();
	}
	
	public void logout() {
		UserHandler.singleton.logout();
	}
	
	public void update(String result) {
//		add(new Label(result));
		if (UserHandler.singleton.isOnline()) {
			content.clear();
			content.add(new Label("User: " + UserHandler.singleton.getUsername()));
			content.add(loggedIn);
			if (UserHandler.singleton.isTestUser()) {
				content.add(manageTests);
				content.add(createSudoku);
			}
			if (UserHandler.singleton.isAdmin()) {
				content.add(handleUsers);
			}
		} else {
			content.clear();
			content.add(loggedOut);
		}
	}
	
	public void setScore(int score) {
		this.score.setText("Score: " + score);
	}
	
	private class ButtonHandler implements ClickHandler {
		private int mButton;
		
		public ButtonHandler(int button) {
			mButton = button;
		}
		
		public void onClick(ClickEvent event) {
			if (mButton == BUTTON_LOGIN) {
				login();
			} else if (mButton == BUTTON_LOGOUT) {
				logout();
			} else if (mButton == REGISTER) {
				PanelHandler.showRegister();
			} else if (mButton == FORGOT) {
				PanelHandler.showForgotPassword();
			} else if (mButton == CHANGE) {
				PanelHandler.showChangePassword();
			} else if (mButton == TEST) {
				PanelHandler.showManageTests();
			} else if (mButton == SUDOKU) {
				PanelHandler.showCreateSudoku();
			} else if (mButton == USERS) {
				PanelHandler.showUserList();
			}
		}
	}
	
	private class SubmitHandler implements FormPanel.SubmitHandler {
		@Override
		public void onSubmit(SubmitEvent event) {
			UserHandler.singleton.login(DOM.getElementProperty(DOM.getElementById("user"), "value"),
					DOM.getElementProperty(DOM.getElementById("password"), "value"));
		}
	}
}
